﻿package fj.ui
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fj.events.FJImageControlEvent;
	
	public class FJImageControl extends MovieClip
	{
		private var _zoom_in:MovieClip;
		private var _zoom_out:MovieClip;
		private var _move_l:MovieClip;
		private var _move_r:MovieClip;
		private var _move_u:MovieClip;
		private var _move_d:MovieClip;
		private var _rotate_l:MovieClip;
		private var _rotate_r:MovieClip;
		
		public function FJImageControl():void
		{
			_zoom_in = getChildByName( "zoom_in" ) as MovieClip;
			_zoom_out = getChildByName( "zoom_out" ) as MovieClip;
			_move_l = getChildByName( "move_l" ) as MovieClip;
			_move_r = getChildByName( "move_r" ) as MovieClip;
			_move_u = getChildByName( "move_u" ) as MovieClip;
			_move_d = getChildByName( "move_d" ) as MovieClip;
			_rotate_l = getChildByName( "rotate_l" ) as MovieClip;
			_rotate_r = getChildByName( "rotate_r" ) as MovieClip;
			
			if( _zoom_in ){
				_zoom_in.addEventListener( MouseEvent.MOUSE_DOWN, zoomInDown );
			}
			if( _zoom_out ){
				_zoom_out.addEventListener( MouseEvent.MOUSE_DOWN, zoomOutDown );
			}
			if( _move_l ){
				_move_l.addEventListener( MouseEvent.MOUSE_DOWN, moveLeftDown );
			}
			if( _move_r ){
				_move_r.addEventListener( MouseEvent.MOUSE_DOWN, moveRightDown );
			}
			if( _move_u ){
				_move_u.addEventListener( MouseEvent.MOUSE_DOWN, moveUpDown );
			}
			if( _move_d ){
				_move_d.addEventListener( MouseEvent.MOUSE_DOWN, moveDownDown );
			}
			if( _rotate_l ){
				_rotate_l.addEventListener( MouseEvent.MOUSE_DOWN, rotateLeftDown );
			}
			if( _rotate_r ){
				_rotate_r.addEventListener( MouseEvent.MOUSE_DOWN, rotateRightDown );
			}
		}
		//downs
		private function zoomInDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, zoomInLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function zoomOutDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, zoomOutLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function moveLeftDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, moveLeftLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function moveRightDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, moveRightLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function moveUpDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, moveUpLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function moveDownDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, moveDownLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function rotateLeftDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, rotateLeftLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		private function rotateRightDown( e:MouseEvent ):void
		{
			addEventListener( Event.ENTER_FRAME, rotateRightLoop );
			stage.addEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
		//loops
		private function zoomInLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.ZOOM, FJImageControlEvent.IN );
			dispatchEvent( evento );
		}
		private function zoomOutLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.ZOOM, FJImageControlEvent.OUT );
			dispatchEvent( evento );
		}
		private function moveLeftLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.MOVE, FJImageControlEvent.LEFT );
			dispatchEvent( evento );
		}
		private function moveRightLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.MOVE, FJImageControlEvent.RIGHT );
			dispatchEvent( evento );
		}
		private function moveUpLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.MOVE, FJImageControlEvent.UP );
			dispatchEvent( evento );
		}
		private function moveDownLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.MOVE, FJImageControlEvent.DOWN );
			dispatchEvent( evento );
		}
		private function rotateLeftLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.ROTATE, FJImageControlEvent.LEFT );
			dispatchEvent( evento );
		}
		private function rotateRightLoop( e:Event ):void
		{
			var evento:FJImageControlEvent = new FJImageControlEvent( FJImageControlEvent.ROTATE, FJImageControlEvent.RIGHT );
			dispatchEvent( evento );
		}
		//up
		private function stageMouseUp( e:MouseEvent ):void{
			removeEventListener( Event.ENTER_FRAME, zoomInLoop );
			removeEventListener( Event.ENTER_FRAME, zoomOutLoop );
			removeEventListener( Event.ENTER_FRAME, moveLeftLoop );
			removeEventListener( Event.ENTER_FRAME, moveRightLoop );
			removeEventListener( Event.ENTER_FRAME, moveUpLoop );
			removeEventListener( Event.ENTER_FRAME, moveDownLoop );
			removeEventListener( Event.ENTER_FRAME, rotateLeftLoop );
			removeEventListener( Event.ENTER_FRAME, rotateRightLoop );
			stage.removeEventListener( MouseEvent.MOUSE_UP, stageMouseUp );
		}
	}
}